package com.songlanyun.modules.officialDonation.controller.api;

import com.songlanyun.common.utils.Constant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.officialDonation.entity.OfficialDonationEntity;
import com.songlanyun.modules.officialDonation.service.OfficialDonationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 官方捐赠
 *
 * @author wuchao
 * @email songlanyun@gmail.com
 * @date 2022-08-02 17:56:54
 */
@RestController
@RequestMapping("/api/official/donation")
public class OfficialDonationApiController extends AbstractApiController {
    @Autowired
    private OfficialDonationService officialDonationService;

    /**
     * 申领记录
     */
    @GetMapping("/page")
    //@RequiresPermissions(":officialdonation:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("user_id", getUserId(true));
        PageUtils page = officialDonationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions(":officialdonation:info")
    public R info(@PathVariable("id") Long id) {
        OfficialDonationEntity officialDonation = officialDonationService.getById(id);

        return R.ok().put("data", officialDonation);
    }

    /**
     * 申领公益积分
     */
    @PostMapping("/apply")
    //@RequiresPermissions(":officialdonation:save")
    public R save(@RequestBody OfficialDonationEntity officialDonation) {
        ValidatorUtils.validateEntity(officialDonation);
        officialDonation.setUserId(getUserId(true));
        officialDonation.setAuditStatus(Constant.AUDITWAIT);
        officialDonationService.save(officialDonation);

        return R.ok();
    }


}
